<%--
Created by IntelliJ IDEA.
User: wang
Date: 2021/3/16/016
Time: 18:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%--添加标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%--在作用域中保存一个当前路径参数ctx--%>
<c:set value="${pageContext.request.contextPath}" var="ctx"/>


<div class="providerAdd">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>供应商新增页面</span>
    </div>
    <form action="#" method="post" id="providerAddForm">
        <!--div的class 为error是验证错误，ok是验证成功-->
        <div class="">
            <label for="providerId">供应商编码：</label>
            <input type="text" name="proCode" id="providerId"/>
            <span id="sp1">*</span>
        </div>
        <div>
            <label for="providerName">供应商名称：</label>
            <input type="text" name="proName" id="providerName"/>
            <span id="sp2">*</span>
        </div>
        <div>
            <label for="people">联系人：</label>
            <input type="text" name="proContact" id="people"/>
            <span id="sp3">*</span>

        </div>
        <div>
            <label for="phone">联系电话：</label>
            <input type="text" name="proPhone" id="phone"/>
            <span id="sp4">*</span>
        </div>
        <div>
            <label for="address">联系地址：</label>
            <input type="text" name="proAddress" id="address"/>
            <span id="sp5"></span>
        </div>
        <div>
            <label for="fax">传真：</label>
            <input type="text" name="proFax" id="fax"/>
            <span id="sp6"></span>
        </div>
        <div>
            <label for="describe">描述：</label>
            <textarea name="proDesc" id="describe" rows="8" cols="40"></textarea>
            <span id="sp7"></span>
        </div>
        <div class="providerAddBtn">
            <!--<a href="#">保存</a>-->
            <!--<a href="providerList.jsp">返回</a>-->
            <input type="button" value="保存" id="btn"/>
            <input type="button" value="返回" onclick="location.href='${ctx}/provider/providerList'"/>
        </div>
    </form>
</div>

<%--新增供应商--%>
<script type="text/javascript">
    $(function () {
        $("#btn").click(function () {
            var flag1 = false;
            var flag2 = false;
            var flag3 = false;
            var flag4 = false;

            //电话号码正则表达式
            var regexPhone = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/;

            //后台获得供应商编码json数组传入前端
            //判断输入的供应商编码是否存在于该供应商编码json数组中
            //var providerList0 = "${providerList}";
            //alert(providerList0);
            // var providerList1=JSON.stringify(providerList0);
            //alert(providerList1);
            /* var providerList2=JSON.parse(



            ${providerList});
            alert(providerList2);*/
            /*
            var providerList =




            ${providerList};
            alert(providerList);
            var proListObj = JSON.parse(providerList);
            var val0 = $("#providerId").val();
            var flag = true;
            alert(proListObj[1].proDesc);
            for (var i in proListObj) {
                if (val0 == proListObj[i].proDesc) {
                    flag = false;
                    break;
                }
            }
            */


            //js验证
            if ($("#providerId").val() == null || $("#providerId").val() == "" || $("#providerId").val() == "null") {
                $("#sp1").html("供应商编码不能为空或null").css("color", "red");
            } else {
                //获得后端的供应商集合的json数据
                var providerList =${providerList};
                //alert(providerList);
                // alert(JSON.stringify(providerList));
                var flag = false;
                var val0 = $("#providerId").val();
                for (var i in providerList) {
                    // console.log("aaaaaaaaaaaa:"+providerList[i].proCode);
                    if (providerList[i].proCode == val0) {
                        flag = true;
                        break;
                    }
                }
                // alert("flag-----" + flag);
                if (flag) {
                    //alert("该供应商编码存在！");
                    $("#sp1").html("供应商编码存在!").css("color", "red");
                } else {
                    //alert("该供应商编码不存在！");
                    $("#sp1").html("供应商编码符合条件").css("color", "green");
                    flag1 = true;
                }
                //异步请求方式验证供应商编码是否存在
                /*$.post(
                    ctx + "/provider/queryProCode",
                    {"proCode": $("#providerId").val()},
                    function (result) {
                        // alert("ajax--result---"+result);
                        if (result) {
                            $("#sp1").html("供应商编码存在，请重新输入！").css("color", "red");
                        } else {
                            $("#sp1").html("供应商编码符合条件").css("color", "green");
                            flag1 = true;
                        }
                    }, "json"
                );*/
            }

            if ($("#providerName").val() == null || $("#providerName").val() == "" || $("#providerName").val() == "null") {
                $("#sp2").html("供应商名称不能为空或null").css("color", "red");
            } else {
                $("#sp2").html("供应商名称符合条件").css("color", "green");
                flag2 = true;


            }

            if ($("#people").val() == null || $("#people").val() == "" || $("#people").val() == "null") {
                $("#sp3").html("联系人不能为空或null").css("color", "red");
            } else {
                $("#sp3").html("联系人符合条件").css("color", "green");
                flag3 = true;
            }

            if ($("#phone").val() == "" || $("#phone").val() == "null") {
                $("#sp4").html("手机号不能为空或null").css("color", "red");
            } else if (!regexPhone.test($("#phone").val())) {
                $("#sp4").html("手机号不符合条件！").css("color", "red");
            } else {
                $("#sp4").html("手机号符合条件").css("color", "green");
                flag4 = true;
            }

            if (flag1 && flag2 && flag3 && flag4) {
                //验证成功
                var data = $("#providerAddForm").serialize();
                //alert(data);
                $.post(
                    ctx + "/provider/providerAdd",
                    data,
                    function (result) {
                        //alert(result);
                        if (result > 0) {
                            alert("新增成功！");
                            location.href = "${ctx}/provider/providerList";
                        } else {
                            alert("新增失败！");
                        }

                    }, "json"
                );
            } else {
                alert("请根据页面提示信息进行修改！");
            }
        });

    });
</script>


